package com.chezai.client.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.chezai.client.bean.UserInfo;

import com.chezai.client.bean.ClassRecord;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "CLASS_RECORD".
*/
public class ClassRecordDao extends AbstractDao<ClassRecord, Long> {

    public static final String TABLENAME = "CLASS_RECORD";

    /**
     * Properties of entity ClassRecord.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property _ID = new Property(0, Long.class, "_ID", true, "_id");
        public final static Property CreateTime = new Property(1, String.class, "createTime", false, "CREATE_TIME");
        public final static Property SignInTime = new Property(2, Long.class, "signInTime", false, "SIGN_IN_TIME");
        public final static Property SignOutTime = new Property(3, Long.class, "signOutTime", false, "SIGN_OUT_TIME");
        public final static Property ClassId = new Property(4, Long.class, "classId", false, "CLASS_ID");
        public final static Property CourseNum = new Property(5, String.class, "courseNum", false, "COURSE_NUM");
        public final static Property SignStatus = new Property(6, int.class, "signStatus", false, "SIGN_STATUS");
        public final static Property TrainPart = new Property(7, int.class, "trainPart", false, "TRAIN_PART");
        public final static Property TrainSubject = new Property(8, String.class, "trainSubject", false, "TRAIN_SUBJECT");
        public final static Property StudyValidLong = new Property(9, long.class, "studyValidLong", false, "STUDY_VALID_LONG");
        public final static Property Mileage = new Property(10, int.class, "mileage", false, "MILEAGE");
        public final static Property Sid = new Property(11, Long.class, "sid", false, "SID");
    }

    private DaoSession daoSession;


    public ClassRecordDao(DaoConfig config) {
        super(config);
    }
    
    public ClassRecordDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"CLASS_RECORD\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: _ID
                "\"CREATE_TIME\" TEXT," + // 1: createTime
                "\"SIGN_IN_TIME\" INTEGER," + // 2: signInTime
                "\"SIGN_OUT_TIME\" INTEGER," + // 3: signOutTime
                "\"CLASS_ID\" INTEGER," + // 4: classId
                "\"COURSE_NUM\" TEXT," + // 5: courseNum
                "\"SIGN_STATUS\" INTEGER NOT NULL ," + // 6: signStatus
                "\"TRAIN_PART\" INTEGER NOT NULL ," + // 7: trainPart
                "\"TRAIN_SUBJECT\" TEXT," + // 8: trainSubject
                "\"STUDY_VALID_LONG\" INTEGER NOT NULL ," + // 9: studyValidLong
                "\"MILEAGE\" INTEGER NOT NULL ," + // 10: mileage
                "\"SID\" INTEGER);"); // 11: sid
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CLASS_RECORD\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, ClassRecord entity) {
        stmt.clearBindings();
 
        Long _ID = entity.get_ID();
        if (_ID != null) {
            stmt.bindLong(1, _ID);
        }
 
        String createTime = entity.getCreateTime();
        if (createTime != null) {
            stmt.bindString(2, createTime);
        }
 
        Long signInTime = entity.getSignInTime();
        if (signInTime != null) {
            stmt.bindLong(3, signInTime);
        }
 
        Long signOutTime = entity.getSignOutTime();
        if (signOutTime != null) {
            stmt.bindLong(4, signOutTime);
        }
 
        Long classId = entity.getClassId();
        if (classId != null) {
            stmt.bindLong(5, classId);
        }
 
        String courseNum = entity.getCourseNum();
        if (courseNum != null) {
            stmt.bindString(6, courseNum);
        }
        stmt.bindLong(7, entity.getSignStatus());
        stmt.bindLong(8, entity.getTrainPart());
 
        String trainSubject = entity.getTrainSubject();
        if (trainSubject != null) {
            stmt.bindString(9, trainSubject);
        }
        stmt.bindLong(10, entity.getStudyValidLong());
        stmt.bindLong(11, entity.getMileage());
 
        Long sid = entity.getSid();
        if (sid != null) {
            stmt.bindLong(12, sid);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, ClassRecord entity) {
        stmt.clearBindings();
 
        Long _ID = entity.get_ID();
        if (_ID != null) {
            stmt.bindLong(1, _ID);
        }
 
        String createTime = entity.getCreateTime();
        if (createTime != null) {
            stmt.bindString(2, createTime);
        }
 
        Long signInTime = entity.getSignInTime();
        if (signInTime != null) {
            stmt.bindLong(3, signInTime);
        }
 
        Long signOutTime = entity.getSignOutTime();
        if (signOutTime != null) {
            stmt.bindLong(4, signOutTime);
        }
 
        Long classId = entity.getClassId();
        if (classId != null) {
            stmt.bindLong(5, classId);
        }
 
        String courseNum = entity.getCourseNum();
        if (courseNum != null) {
            stmt.bindString(6, courseNum);
        }
        stmt.bindLong(7, entity.getSignStatus());
        stmt.bindLong(8, entity.getTrainPart());
 
        String trainSubject = entity.getTrainSubject();
        if (trainSubject != null) {
            stmt.bindString(9, trainSubject);
        }
        stmt.bindLong(10, entity.getStudyValidLong());
        stmt.bindLong(11, entity.getMileage());
 
        Long sid = entity.getSid();
        if (sid != null) {
            stmt.bindLong(12, sid);
        }
    }

    @Override
    protected final void attachEntity(ClassRecord entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public ClassRecord readEntity(Cursor cursor, int offset) {
        ClassRecord entity = new ClassRecord( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // _ID
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // createTime
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // signInTime
            cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3), // signOutTime
            cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4), // classId
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // courseNum
            cursor.getInt(offset + 6), // signStatus
            cursor.getInt(offset + 7), // trainPart
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // trainSubject
            cursor.getLong(offset + 9), // studyValidLong
            cursor.getInt(offset + 10), // mileage
            cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11) // sid
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, ClassRecord entity, int offset) {
        entity.set_ID(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setCreateTime(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setSignInTime(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setSignOutTime(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
        entity.setClassId(cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4));
        entity.setCourseNum(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setSignStatus(cursor.getInt(offset + 6));
        entity.setTrainPart(cursor.getInt(offset + 7));
        entity.setTrainSubject(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setStudyValidLong(cursor.getLong(offset + 9));
        entity.setMileage(cursor.getInt(offset + 10));
        entity.setSid(cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(ClassRecord entity, long rowId) {
        entity.set_ID(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(ClassRecord entity) {
        if(entity != null) {
            return entity.get_ID();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(ClassRecord entity) {
        return entity.get_ID() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getUserInfoDao().getAllColumns());
            builder.append(" FROM CLASS_RECORD T");
            builder.append(" LEFT JOIN USER_INFO T0 ON T.\"SID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected ClassRecord loadCurrentDeep(Cursor cursor, boolean lock) {
        ClassRecord entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        UserInfo userInfo = loadCurrentOther(daoSession.getUserInfoDao(), cursor, offset);
        entity.setUserInfo(userInfo);

        return entity;    
    }

    public ClassRecord loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<ClassRecord> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<ClassRecord> list = new ArrayList<ClassRecord>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<ClassRecord> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<ClassRecord> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
